package com.sie.zhq.module;

import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;

import com.sie.zhq.constconfig.ErrorDefine;
import com.sie.zhq.tool.ByteTool;
import com.sie.zhq.tool.Timer;

/**
 * Created by zhq on 2017/1/17.
 */

public class FingerPrintRunnable extends ModuleRunnable {

    private final static String TAG = "FingerPrintRunnable";

    private static Action action;

    private static TesoFingerprintModule tesoFingerPrintModule = TesoFingerprintModule.getInstance();

    public static enum Action {
        READ_VER,
        READ_FEATURE,
        GET_PIC
    }

    ;

    public FingerPrintRunnable(Action action, final DeviceCallback callback, final int timeOutMs, final boolean isOnMain) {
        this.callback = callback;
        this.timeOutMs = timeOutMs;
        this.isOnMain = isOnMain;
        this.action = action;

    }

    @Override
    public void stopRun() {
        this.runFlag = false;
        tesoFingerPrintModule.setStopRun(true);
    }

    @Override
    public void run() {
        synchronized (this) {
            Timer tm = null;
            if (timeOutMs > 0) {
                tm = new Timer(timeOutMs);
                tm.start();
            }

            tesoFingerPrintModule.PowerOn();
            tesoFingerPrintModule.init();
            SystemClock.sleep(1200);
            deliverResult(callback, DeviceInterface.Result.START, null, isOnMain);
            try {
                while (runFlag) {
                    byte[] data = new byte[60000];
                    int[] len = new int[1];
                    if (timeOutMs > 0) {
                        if (tm.timeOut()) {
                            Log.e(TAG, "tm.timeOut!!");
                            Bundle bundle = new Bundle();
                            bundle.putInt("code", ErrorDefine.FINGERPRINT_FREATURE_TIMEOUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.FINGERPRINT_FREATURE_TIMEOUT));
                            deliverResult(callback, DeviceInterface.Result.ERROR, bundle, isOnMain);
                            break;
                        }
                    }
                    if (action == Action.READ_FEATURE) {
                        tesoFingerPrintModule.flush();
                        tesoFingerPrintModule.lightOn();
                        int  res = tesoFingerPrintModule.getFeature(data, len);
                        Log.e("res",res+"---");
                        if ( res == 0) {
                            Log.e(TAG, ByteTool.bytearrayToHexString(data, len[0]));
                            Bundle bundle = new Bundle();
                            bundle.putString("data", ByteTool.bytearrayToHexString(data, len[0]));
                            deliverResult(callback, DeviceInterface.Result.SUCCEED, bundle, isOnMain);
                            tesoFingerPrintModule.lightOff();
                            return;
                        } else {
                            SystemClock.sleep(100);
                            tesoFingerPrintModule.lightOff();
                            continue;
                        }

                    }


                    if(action== Action.GET_PIC){
//                        SystemClock.sleep(500);
                        tesoFingerPrintModule.flush();
                        tesoFingerPrintModule.lightOn();
                        int  res = tesoFingerPrintModule.checkFingerPicture(data,len);
                        Log.e("check_res",res+"---");
                        if ( res == 0) {
                            byte[] fingerData = new byte[1024];
                            int[] len1 = new int[1];
                            res = tesoFingerPrintModule.getFingerPicture(data,len);
                                  tesoFingerPrintModule.getFeature(fingerData, len1);
                            Log.e("get_res",res+"---");
                            if(res==0) {
                              //  Log.e(TAG, ByteTool.bytearrayToHexString(data, len[0]));
                                Log.e(TAG,"len=="+len[0]);
                                byte[] buff = new byte[len[0]-28];
                                Bundle bundle = new Bundle();
                                System.arraycopy(data,28,buff,0,len[0]-28);
                                bundle.putString("data", ByteTool.bytearrayToHexString(fingerData, len1[0]));
                                bundle.putInt("zip",1);
                                bundle.putByteArray("imgBuff",buff);
                                deliverResult(callback, DeviceInterface.Result.SUCCEED, bundle, isOnMain);
                                tesoFingerPrintModule.lightOff();
                                return;
                            }

                        }

                    }


                    if (action == Action.READ_VER) {
                        if (tesoFingerPrintModule.readVersion(data, len) == 0) {
                            Log.e("ver", ByteTool.bytearrayToHexString(data, len[0]));
                            Bundle bundle = new Bundle();
                            bundle.putString("data", ByteTool.bytearrayToHexString(data, len[0]));
                            deliverResult(callback, DeviceInterface.Result.SUCCEED, bundle, isOnMain);
                            return;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
//                HKBaseApplication.saveCatchInfo2File(e);
            } finally {
                deliverResult(callback, DeviceInterface.Result.COMPLETED, new Bundle(), isOnMain);
                tesoFingerPrintModule.PowerOff();
                tesoFingerPrintModule.lightOff();
                return;
            }
        }
    }
}
